//
//  EmoticonVC.swift
//  测试2-表情键盘
//
//  Created by shmily on 15/8/6.
//  Copyright © 2015年 shmilyAshen. All rights reserved.
//

import UIKit

/// 显示表情键盘的控制器
class EmoticonVC: UIViewController {
    
    
    /// 按钮点击事件
    func toolbarClick(sender: UIBarButtonItem) {
        print(sender.tag)
    }
    
    private func prepareUI() {
        view.addSubview(toolbar)
        view.addSubview(collectView)
        
        toolbar.translatesAutoresizingMaskIntoConstraints = false
        collectView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[cv]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["cv":collectView]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[tb]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["tb":toolbar]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[cv]-0-[tb(44)]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["cv":collectView,"tb":toolbar]))
    }
    
    private func prepareCollection() {
        
    }
    
    
    
    /// 构造函数
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        
        prepareUI()
        prepareCollection()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    /// 懒加载
    private lazy var collectView: UICollectionView = UICollectionView(frame: CGRectZero, collectionViewLayout:  collectionLayout())
    
    private lazy var toolbar: UIToolbar = {
        let bar = UIToolbar()
        var items = [UIBarButtonItem]()
        let names = ["最近","默认","emoji","浪小花"]
        var index = 0
        for name in names {
            let bar = UIBarButtonItem(title: name, style: UIBarButtonItemStyle.Plain, target: self, action: "toolbarClick:")
            bar.tag = index++
           items.append(bar)
            items.append(UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil))
        }
        items.removeLast()
        bar.items = items
        
        return bar
    }()

}

/// coleciton的 cell
class collectCell: UICollectionViewCell {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 布局 UI
    private func prepareUI() {
       contentView.addSubview(currentBtn)
        
        
    }
    
    //懒加载
    private lazy var currentBtn: UIButton = {
        let btn = UIButton()
        btn.backgroundColor = UIColor.whiteColor()
        btn.frame = CGRectInset(self.contentView.bounds, 5, 5)
        return btn
    }()
}

/// collectionView 的流式布局
class collectionLayout: UICollectionViewFlowLayout {
    
    
    override func prepareLayout() {
        let width = UIScreen.mainScreen().bounds.width / 7
//        let height = collectionView!.bounds.height / 3
        itemSize = CGSize(width: width, height: width)

        minimumInteritemSpacing = 0
        minimumLineSpacing = 0
        scrollDirection = UICollectionViewScrollDirection.Horizontal
    }
}
